﻿using System;
using System.Data;
using System.Text;


namespace WinFormClient
{
    public class Service
    {
        private string _tableName;
        private MethodCollection _methods;

        public Service(string tableName, MethodCollection col)
        {
            this._tableName = tableName;
            this._methods = col;
        }

        public void Builder()
        {
            string content
            = this.Header()
            + this.Methods()
            + this.Footer()
            ;

            Tools.TextSaveToFile(content, "Service", this._tableName + "Service.cs");
        }

        #region header
        protected string Header()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("#region namespace" + Environment.NewLine);
            sb.Append("using System;" + Environment.NewLine);
            sb.Append("using System.Collections.Generic;" + Environment.NewLine);
            sb.Append("using System.Text;" + Environment.NewLine);
            sb.Append("using IBatisNet.DataAccess;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Interface;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Core;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Model;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Parameters;" + Environment.NewLine);
            sb.Append("#endregion" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("namespace " + MapXml.NameSpace + ".Service" + Environment.NewLine);
            sb.Append("{" + Environment.NewLine);
            sb.Append("    public partial class " + this._tableName + "Service" + Environment.NewLine);
            sb.Append("    {" + Environment.NewLine);
            sb.Append("        private Interface.I"+this._tableName+ " " + Tools.FieldConvert(this._tableName)+ ";" + Environment.NewLine);
            sb.Append("        private static " + this._tableName + "Service _instance = new " + this._tableName + "Service();" + Environment.NewLine);
            sb.Append("        private DaoManager _daoManager = null;" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("        public static " + this._tableName + "Service GetInstance()" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("            return _instance;" + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("        private " + this._tableName + "Service()" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("            _daoManager = Core.DaoLoader.GetInstance().DaoManager;" + Environment.NewLine);
            sb.Append("            " + Tools.FieldConvert(this._tableName) + " = _daoManager.GetDao(typeof(I" + this._tableName + ")) as I" + this._tableName + ";" + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        #region methods
        protected string Methods()
        {
            StringBuilder sb = new StringBuilder();
            Method mth = null;
            for (int i = 0; i < this._methods.Count;i++ )
            {
                mth = this._methods[i];
                sb.Append(Environment.NewLine);
                sb.Append("        public ");
                if (mth.AccessMethod == DataAccessMethod.ExecuteQueryForList)
                {
                    sb.Append("IList<" + mth.ReturnType + ">");
                }
                else
                {
                    sb.Append(mth.ReturnType);
                }


                #region 2010.4.19 by abyliver.zhang
                if (mth.MethodName.Contains("AnyWhere"))
                {
                    sb.Append(" " + mth.MethodName + "(string strQueryString)" + Environment.NewLine);
                    sb.Append("        {" + Environment.NewLine);
                    sb.Append("            " + (mth.ParameterType + " " + mth.ParameterName) + " = new " + mth.ParameterType + "(); " + Environment.NewLine);
                    sb.Append("            " + mth.ParameterName + ".UnlimitedQuery = strQueryString; " + Environment.NewLine);
                }
                else
                {
                    sb.Append(" " + mth.MethodName + "(" + ((!String.IsNullOrEmpty(mth.ParameterType)) ? (mth.ParameterType + " " + mth.ParameterName) : "") + ")" + Environment.NewLine);
                    sb.Append("        {" + Environment.NewLine);
                }
                #endregion

                sb.Append("            return ");
                sb.Append("this." + Tools.FieldConvert(this._tableName) + "." + mth.MethodName + "(");
                sb.Append(((!String.IsNullOrEmpty(mth.ParameterType))?mth.ParameterName : "") + ");" + Environment.NewLine);

                sb.Append("        }" + Environment.NewLine);
                sb.Append(Environment.NewLine);
            }

            return sb.ToString();
        }
        #endregion

        #region footer
        protected string Footer()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("    }" + Environment.NewLine);
            sb.Append("}" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        public void Dispose()
        {
            this._methods.Dispose();
        }
    }
}
